package system.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import system.manage.SystemManage;
import system.model.SystemInfor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SystemAction extends ActionSupport {
	private SystemManage systemManage=null;
	private String logo=null;
	private String webName=null;
	private String bottomContent=null;
	private String startTime=null;
	private String endTime=null;
	private SystemInfor systemInfor=null;
	private File[] upload;
	private String[] uploadContentType;
	private String[] uploadFileName;
	private String[] adImage;
	private String[] url;
	private String webEmailPass=null;
	private String webEmail=null;
	
	
	@Override
	public String execute() throws Exception {
		
		
		return this.SUCCESS;
	}
    public String systemInfor() throws Exception {
    	String path = ServletActionContext.getRequest().getRealPath(
			"/WEB-INF/classes/system.xml");
    	systemInfor=systemManage.systemInfor(path);
		return "SystemInfor";
	}
 public String addSystemInfor() throws Exception {
	 SystemInfor systemInfor=new SystemInfor();
		systemInfor.setLogo((logo==null)?"":logo);
		if(webName!=null)
	      {
	    	  try {
	    		  webName=new String(webName.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			} 
	      }
		systemInfor.setWebName((webName==null)?"":webName);
		if(bottomContent!=null)
	      {
	    	  try {
	    		  bottomContent=new String(bottomContent.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			} 
	      }
		systemInfor.setBottomContent((bottomContent==null)?"":bottomContent);
		systemInfor.setStartTime((startTime==null)?"":startTime);
		systemInfor.setEndTime((endTime==null)?"":endTime);
		systemInfor.setWebEmail((webEmail==null)?"":webEmail);
		systemInfor.setWebEmailPass((webEmailPass==null)?"":webEmailPass);
		String path = ServletActionContext.getRequest().getRealPath(
		"/WEB-INF/classes/system.xml");
    	systemManage.addSystemInfor(systemInfor,path);
    	
    	  ActionContext actionContext = ActionContext.getContext();
          actionContext.getApplication().put("systemInfor", systemInfor);
		return "AddSystemInfor";
	}
 /**
  * 添加广告
  * @return
  */
    public String addAd()throws Exception
    {
    	
    	 SystemInfor systemInfor=new SystemInfor();

 		File[] files = getUpload();
        List list=new ArrayList();
 		InputStream is = null;
 		OutputStream os = null;
 		try {
 			for (int i = 0; i < files.length; i++) {
 				is = new FileInputStream(upload[i]);
 				String path = ServletActionContext.getRequest().getRealPath(
 						"/uploadFile/ad");
 				String image =adImage[i]+"."
 						+  uploadFileName[i].substring(uploadFileName[i]
 								.lastIndexOf(".") + 1, uploadFileName[i]
 								.length());
 				os = new FileOutputStream(new File(path, image));
 				byte[] buffer = new byte[400];
 				int length = 0;
 				while ((length = is.read(buffer)) > 0) {
 					os.write(buffer, 0, length);
 				}
 				list.add(image);
 			}
 			is.close();
 			os.close();
 			
 		} catch (Exception e) {
 			e.printStackTrace();
 			this.addActionError("上传失败！");
 		}
 		systemInfor.setAdImage(list);
 		String path = ServletActionContext.getRequest().getRealPath(
		"/WEB-INF/classes/system.xml");
 		systemManage.addAd(systemInfor,adImage,path,url);
 		this.addActionMessage("上传成功!");
 		
 		 ActionContext actionContext = ActionContext.getContext();
 		systemInfor= systemManage.systemInfor(path);
         actionContext.getApplication().put("systemInfor",systemInfor);
    	return "AddAd";
    }
	public String getLogo() {
		return logo;
	}


	public void setLogo(String logo) {
		this.logo = logo;
	}


	public String getWebName() {
		return webName;
	}


	public void setWebName(String webName) {
		this.webName = webName;
	}


	public String getBottomContent() {
		return bottomContent;
	}


	public void setBottomContent(String bottomContent) {
		this.bottomContent = bottomContent;
	}


	public String getStartTime() {
		return startTime;
	}


	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}


	public String getEndTime() {
		return endTime;
	}


	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public SystemManage getSystemManage() {
		return systemManage;
	}
	public void setSystemManage(SystemManage systemManage) {
		this.systemManage = systemManage;
	}
	public SystemInfor getSystemInfor() {
		return systemInfor;
	}
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String[] getAdImage() {
		return adImage;
	}
	public void setAdImage(String[] adImage) {
		this.adImage = adImage;
	}
	public String getWebEmailPass() {
		return webEmailPass;
	}
	public void setWebEmailPass(String webEmailPass) {
		this.webEmailPass = webEmailPass;
	}
	public String getWebEmail() {
		return webEmail;
	}
	public void setWebEmail(String webEmail) {
		this.webEmail = webEmail;
	}
	public String[] getUrl() {
		return url;
	}
	public void setUrl(String[] url) {
		this.url = url;
	}


	

}
